// 旋转操作
// 登录页面，点击注册，旋转到到注册
function toRegister() {
  // $(".register").css("opacity", 1);
  $(".forgetPassword").css("opacity", 0);
  $(".back").css("opacity", 1);
  $(".container").css("transform", "rotate3d(0, 1, 0, 90deg)");
}
// 登录页面，点击忘记密码，跳转到忘记密码页面
function toForgetPd() {
  // $(".forgetPassword").css("opacity", 1);
  $(".register").css("opacity", 0);
  $(".back").css("opacity", 1);
  $(".container").css("transform", "rotate3d(0, 1, 0, -90deg)");
}
// 注册页面，点击去登录，旋转到登录页面
function toLogin() {
  $(".register .testCode input").val("");
  $(".forgetPassword .testCode input").val("");
  $("#reg-ipt1").val("");
  $("#reg-ipt2").val("");
  $(".forgetPassword").css("opacity", 1);
  $(".register").css("opacity", 1);
  $(".back").css("opacity", 0);
  $(".container").css("transform", "rotate3d(0, 1, 0, 0)");
}
//实现登录页面背景图，随着刷新而改变
// background: url(https://i.loli.net/2021/09/28/WVfYCghcbILaRs2.jpg) no-repeat;
/* background: url(https://i.loli.net/2021/09/29/eVyLG4RzpZP7BOw.jpg) no-repeat; */
function refreshBackImg() {
  //定义一个数组，用来存放图片地址
  let imgUrl = [
    "https://i.loli.net/2021/09/28/WVfYCghcbILaRs2.jpg",
    "https://i.loli.net/2021/09/29/eVyLG4RzpZP7BOw.jpg",
    "https://i.loli.net/2021/09/30/oINsDTLFSnwE1CX.jpg",
    "https://i.loli.net/2021/09/30/odf2JycGnD6OziZ.jpg",
  ];
  //生成一个随机下标
  let randomIndex = parseInt(Math.random() * imgUrl.length);
  // console.log(randomIndex);
  $("body").css({
    background: `url(${imgUrl[randomIndex]}) no-repeat`,
    "background-size": "cover",
  });
  //改变颜色，同理
  let botColor = [
    "rgb(104, 102, 102)",
    "rgba(235, 231, 231, 0.753)",
    "#262627ce",
    "rgba(235, 231, 231, 0.753)",
  ];
  $(".bottom").css("color", `${botColor[randomIndex]}`);
}
refreshBackImg();
//随机获取六位的验证码
function randomNum() {
  let num = parseInt(Math.random() * (1000000 - 100000 + 1));
  // console.log(num + 100000);
  let randomNum = num + 100000;
  // console.log($(".register .testCode input"));
  return randomNum;
}
function testCode1() {
  randomNum;
  $(".register .testCode input").val(randomNum);
}
function testCode2() {
  randomNum;
  $(".forgetPassword .testCode input").val(randomNum);
}
// axios请求
// const axios = require("axios");
//注册接口 http://jx.xuzhixiang.top/ap/api/reg.php
//设置账号和密码的正则验证
function phoneNum(phoneVal) {
  isPhone = false;
  let reg = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/; //用户名是手机号
  if (reg.test(phoneVal)) {
    isPhone = true;
    console.log("手机号格式正确");
  } else {
    isPhone = false;
    console.log("手机号格式不正确");
  }
}
function password(passwordVal) {
  isPassword = false;
  let reg = /^\S{6,10}$/; //密码6到10位
  if (reg.test(passwordVal)) {
    isPassword = true;
    console.log("密码格式正确");
  } else {
    isPassword = false;
    console.log("密码由6到10位非空格字符组成");
  }
}
$("#regBtn").click(function () {
  phoneNum($("#reg-ipt1").val());
  console.log(isPhone);
  password($("#reg-ipt2").val());
  console.log(isPassword);
  if (
    isPhone == true &&
    isPassword == true &&
    $(".register .testCode input").val()
  ) {
    $.get(
      "http://jx.xuzhixiang.top/ap/api/reg.php",
      {
        username: $("#reg-ipt1").val(),
        password: $("#reg-ipt2").val(),
      },
      (data) => {
        console.log(data);
        if (data.code == 1) {
          alert(data.msg);
          toLogin();
        } else {
          alert(data.msg);
        }
      }
    );
  } else {
    alert("不能注册，请输入正确的手机号、密码和验证码");
  }
});
//登录验证
// 登录API http://jx.xuzhixiang.top/ap/api/login.php
$("#logBtn").click(function () {
  phoneNum($("#log-ipt1").val());
  console.log(isPhone);
  password($("#log-ipt2").val());
  console.log(isPassword);
  if (isPhone == true && isPassword == true) {
    $.get(
      "http://jx.xuzhixiang.top/ap/api/login.php",
      {
        username: $("#log-ipt1").val(),
        password: $("#log-ipt2").val(),
      },
      (data) => {
        console.log(data);
        if (data.code == 1) {
          alert(data.msg);
          location.href = "index.html";
          localStorage.setItem("username", data.data.username);
          localStorage.setItem("token", data.data.token);
          localStorage.setItem("uid", data.data.id);
        } else {
          alert(data.msg);
        }
      }
    );
  } else {
    alert("不能登录，请输入正确的手机号和密码");
  }
});
// //记录本地内存里的 username，如果存在就直接登录
// let username = localStorage.getItem("username");
// console.log(username);
// confirm("存在用户" + username + "是否直接登录");
